﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibPlateAnalysis;
using ImageAnalysis;
using System.IO;

namespace HCSAnalyzer.Classes.General_Types
{

    public enum eImagingPlatformType { OPERETTA, MANUAL };

    public class cImageAccessor
    {

        public string InitialPath = "";
        public eImagingPlatformType ImagingPlatformType;
        public int NumberOfChannels = 1;
        public int Field = 1;


        public cImageAccessor(eImagingPlatformType ImagingPlatformType)
        {
            //this.InitialPath = InitialPath;
            this.ImagingPlatformType = ImagingPlatformType;
        }

        public string GetImageFileName(object Sender)
        {
            string FileName = "";

            // The image file name is composed as follows:
            // r02c07f01p01rc1-ch1sk1fk1fl1.tiff
            // r=row, c=coloumn, f=field, p=plane, rc= record (channel)
            // (second part of the file name currently not in use)
            if (this.ImagingPlatformType == eImagingPlatformType.OPERETTA)
            {
                if (Sender.GetType() == typeof(cSingleBiologicalObject))
                {
                    cSingleBiologicalObject SingleBiologicalObject = (cSingleBiologicalObject)Sender;

                  //  string CurrDir = Directory.GetCurrentDirectory();
                  //  Directory.SetCurrentDirectory(InitialPath);

                    IEnumerable<string> ListDir = Directory.EnumerateDirectories(InitialPath, "*" + SingleBiologicalObject.AssociatedWell.AssociatedPlate.GetName() + "__*");
                    if (ListDir.Count() != 1 ) return "";

                    string NewDir = ListDir.ElementAt(0);

                    for (int Channel = 1; Channel <= NumberOfChannels; Channel++)
                    {
                        // get the well information
                        int c = SingleBiologicalObject.AssociatedWell.GetPosX();
                        int r = SingleBiologicalObject.AssociatedWell.GetPosY();
                        int f = SingleBiologicalObject.ImageField; // field
                        int p = 1; // plane

                        string Name = "r" + r.ToString("D2") +
                                      "c" + c.ToString("D2") +
                                      "f" + f.ToString("D2") +
                                      "p" + p.ToString("D2") +
                                      "rc" + Channel.ToString() +
                                      "-ch1sk1fk1fl1.tiff";

                        FileName += NewDir + "\\Images\\" + Name + "|";

                        //cImage NewImage = 
                    }
                    
                  //  FileName = NewDir;

                 //   Directory.SetCurrentDirectory(CurrDir);
                }
                else if(Sender.GetType() == typeof(cWell))
                {
                    cWell WellObject = (cWell)Sender;


                  //  IEnumerable<string> ListDir = Directory.EnumerateDirectories(InitialPath, "*" + WellObject.AssociatedPlate.GetName() + "__*");
                    IEnumerable<string> ListDir = Directory.EnumerateDirectories(InitialPath, "*Plate4__*");
                    if (ListDir.Count() != 1) return "";


                    string NewDir = ListDir.ElementAt(0);


                    for (int Channel = 1; Channel <= NumberOfChannels; Channel++)
                    {
                        // get the well information
                        int c = WellObject.GetPosX();
                        int r = WellObject.GetPosY();
                        int f = this.Field; // field
                        int p = 1; // plane

                        string Name = "r" + r.ToString("D2") +
                                      "c" + c.ToString("D2") +
                                      "f" + f.ToString("D2") +
                                      "p" + p.ToString("D2") +
                                      "rc" + Channel.ToString() +
                                      "-ch1sk1fk1fl1.tiff";

                        FileName += NewDir + "\\Images\\" + Name + "|";

                        //cImage NewImage = 
                    }

                }
            }
            else if (this.ImagingPlatformType == eImagingPlatformType.MANUAL)
            {
                if (Sender.GetType() == typeof(cSingleBiologicalObject))
                {
                    cSingleBiologicalObject SingleBiologicalObject = (cSingleBiologicalObject)Sender;
                    FileName += SingleBiologicalObject.AssociatedWell.Info;
                }
                if (Sender.GetType() == typeof(cWell))
                {
                    cWell Well = (cWell)Sender;
                    FileName += Well.Info;
                }
            }
            return FileName;
        }

    }
}
